package com.ruoyi.eamp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.eamp.domain.EampMeetingPeople;
import com.ruoyi.eamp.domain.vo.EampMeetingAndroomVO;
import com.ruoyi.eamp.domain.vo.EampMeetingVO;
import com.ruoyi.eamp.mapper.EampMeetingPeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eamp.mapper.EampMeetingMapper;
import com.ruoyi.eamp.domain.EampMeeting;
import com.ruoyi.eamp.service.IEampMeetingService;

/**
 * 会议记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class EampMeetingServiceImpl implements IEampMeetingService {
    @Autowired
    private EampMeetingMapper eampMeetingMapper;
    @Autowired
    private EampMeetingPeopleMapper eampMeetingPeopleMapper;

    /**
     * 查询会议记录
     *
     * @param meetingId 会议记录主键
     * @return 会议记录
     */
    @Override
    public EampMeetingVO selectEampMeetingByMeetingId(Long meetingId) {

        EampMeetingVO eampMeetingVO = eampMeetingMapper.selectEampMeetingByMeetingId(meetingId);

        List<EampMeetingPeople> peopleList = eampMeetingVO.getPeopleList();

        List<Long> shareList=new ArrayList<>();

        for (EampMeetingPeople eve:peopleList) {
            if(eve.getPeopleType()==2){
                shareList.add(eve.getPeopleId());
            }

        }

        List<Long> attendeesList=new ArrayList<>();

        for (EampMeetingPeople eve:peopleList) {
            if(eve.getPeopleType()==1){
                attendeesList.add(eve.getPeopleId());
            }

        }

        Long contectpeople=5L;
        for (EampMeetingPeople eve:peopleList) {
            if(eve.getPeopleType()==3){
                contectpeople =eve.getPeopleId();
            }
        }
        Long compere=6L;
        for (EampMeetingPeople eve:peopleList) {
            if(eve.getPeopleType()==0){
                compere =eve.getPeopleId();
            }
        }






        eampMeetingVO.setSharer(shareList);
        eampMeetingVO.setAttendees(attendeesList);
        eampMeetingVO.setContectpeople(contectpeople);
        eampMeetingVO.setCompere(compere);


        return eampMeetingVO;
    }

    /**
     * 查询会议记录列表
     *
     * @param eampMeeting 会议记录
     * @return 会议记录
     */
    @Override
    public List<EampMeetingAndroomVO> selectEampMeetingList(EampMeeting eampMeeting) {
        return eampMeetingMapper.selectEampMeetingList(eampMeeting);
    }

    /**
     * 新增会议记录
     *
     * @param eampMeeting 会议记录
     * @return 结果
     */
    @Override
    public int insertEampMeeting(EampMeetingVO eampMeeting) {
        List<EampMeeting> meetings = eampMeetingMapper.slecedatetime(eampMeeting.getMeetingRoomId());

        eampMeeting.setCreateTime(DateUtils.getNowDate());
        List<Date> startAndEnd = eampMeeting.getStartAndEnd();
        eampMeeting.setStartDate(startAndEnd.get(0));
        eampMeeting.setEndDate(startAndEnd.get(1));

        Date meetingBegin = eampMeeting.getStartDate();
        Date meetingEnd = eampMeeting.getEndDate();
        boolean a = false;
        int num = 0;

        for (EampMeeting meeting : meetings) {
            Date endDate = meeting.getEndDate();
            Date startDate = meeting.getStartDate();
            if (meetingEnd.before(startDate) || meetingBegin.after(endDate)) {
                num += 1;
            }
        }

        if (num == meetings.size()) {
            a = true;
        }
        if (a) {
            //新增会议,返回会议id
            int i = eampMeetingMapper.insertEampMeeting(eampMeeting);
            Long meetingId = eampMeeting.getMeetingId();
            //新增人员
            List<Long> sharer = eampMeeting.getSharer();
            for (Long eve : sharer) {
                EampMeetingPeople eampMeetingPeople = new EampMeetingPeople();
                eampMeetingPeople.setMeetingId(meetingId);
                eampMeetingPeople.setPeopleId(eve);
                eampMeetingPeople.setPeopleType(2L);
                eampMeetingPeopleMapper.insertEampMeetingPeople(eampMeetingPeople);
            }
            ;
            //zhuchiren
            //主持人
            Long compere = eampMeeting.getCompere();
            EampMeetingPeople eampMeetingPeople = new EampMeetingPeople();
            eampMeetingPeople.setMeetingId(meetingId);
            eampMeetingPeople.setPeopleId(compere);
            eampMeetingPeople.setPeopleType(0L);
            eampMeetingPeopleMapper.insertEampMeetingPeople(eampMeetingPeople);

            Long comtectpeople = eampMeeting.getContectpeople();
            EampMeetingPeople eampMeetingPeopleee = new EampMeetingPeople();
            eampMeetingPeopleee.setMeetingId(meetingId);
            eampMeetingPeopleee.setPeopleId(comtectpeople);
            eampMeetingPeopleee.setPeopleType(3L);
            eampMeetingPeopleMapper.insertEampMeetingPeople(eampMeetingPeopleee);

            // eampMeeting.getAsda();

            List<Long> attendees = eampMeeting.getAttendees();
            for (Long evee : attendees) {
                EampMeetingPeople eampMeetingPeoplee = new EampMeetingPeople();
                eampMeetingPeoplee.setMeetingId(meetingId);
                eampMeetingPeoplee.setPeopleId(evee);
                eampMeetingPeoplee.setPeopleType(1L);
                eampMeetingPeopleMapper.insertEampMeetingPeople(eampMeetingPeoplee);
            }
            ;


            return i;
        } else {
            return -1;
        }

    }

    /**
     * 修改会议记录
     *
     * @param eampMeeting 会议记录
     * @return 结果
     */
    @Override
    public int updateEampMeeting(EampMeeting eampMeeting) {
        return eampMeetingMapper.updateEampMeeting(eampMeeting);
    }

    /**
     * 批量删除会议记录
     *
     * @param meetingIds 需要删除的会议记录主键
     * @return 结果
     */
    @Override
    public int deleteEampMeetingByMeetingIds(Long[] meetingIds) {
        return eampMeetingMapper.deleteEampMeetingByMeetingIds(meetingIds);
    }

    /**
     * 删除会议记录信息
     *
     * @param meetingId 会议记录主键
     * @return 结果
     */
    @Override
    public int deleteEampMeetingByMeetingId(Long meetingId) {
        return eampMeetingMapper.deleteEampMeetingByMeetingId(meetingId);
    }
}
